package com.webinsky.web.resource;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.webinsky.article.service.ArticleManagement;
import com.webinsky.common.proj.Lang;
import com.webinsky.domain.resource.ResType;
import com.webinsky.service.resource.ResManagement;
import com.webinsky.web.data.Response;
import com.webinsky.web.data.StatisticsWorkStation;

import reactor.core.publisher.Mono;


@Component
public class StatisticsResource {
	@Autowired
	private ResManagement resManagement;
	@Autowired
	private ArticleManagement articleManagement;
	
	public Mono<ServerResponse> statisticsWorkStation(ServerRequest request){
		Optional<String> optLang = request.queryParam("lang");
		if ( optLang.isPresent() ){
			int imgCount = resManagement.getCountByLang(Lang.parse(optLang.get()), ResType.Image);
			int vdoCount = resManagement.getCountByLang(Lang.parse(optLang.get()), ResType.Video);
			int moldArticleCount = articleManagement.getArticleCountBySiteIdAndLang(1, Lang.parse(optLang.get()));
			int pressArticleCount = articleManagement.getArticleCountBySiteIdAndLang(2, Lang.parse(optLang.get()));
			StatisticsWorkStation s = new StatisticsWorkStation();
			s.setImageCount(imgCount);
			s.setVideoCount(vdoCount);
			s.setPressArticleCount(pressArticleCount);
			s.setMoldArticleCount(moldArticleCount);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(s, Response.STATUS_OK, ""));
		}
		return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, "参数问题"));
		
	}
	
}
